package www.com.library.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import www.com.library.app.Logger;

/**
 * 字符串格式转换器，价格，小数和时间格式
 * 
 * @author Administrator
 *
 */
public class StringFormatter {

	/**
	 * 时间格式
	 * @author Administrator
	 *
	 */
	private class TimeZoneDateFormat {
		public SimpleDateFormat mDF_Time = new SimpleDateFormat("HH:mm:ss");
		public SimpleDateFormat mDF_TimeSimple = new SimpleDateFormat("HH:mm");
		public SimpleDateFormat mDF_Date = new SimpleDateFormat("yyyy-MM-dd");
		public SimpleDateFormat mDF_DateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		public SimpleDateFormat mDF_DateTimeHHMM = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		public SimpleDateFormat mDF_SimpleDateTime = new SimpleDateFormat("MM/dd HH:mm");
		public SimpleDateFormat mDF_SimpleDate = new SimpleDateFormat("MM-dd HH:mm");
		public SimpleDateFormat mDF_Date2 = new SimpleDateFormat("yyyy/MM/dd");
		public SimpleDateFormat mDF_Date3 = new SimpleDateFormat("yyyy/MM/dd HH:mm");

		public void initTime(int zoneTime){
			setZoneTime(zoneTime);
			Logger.e("stringformatter zone = " + mZoneTime);
			TimeZone tz = TimeZone.getTimeZone(mZoneTime);
			TimeZone.setDefault(tz);
			mDF_Time.setTimeZone(tz);
			mDF_TimeSimple.setTimeZone(tz);
			mDF_Date.setTimeZone(tz);
			mDF_DateTime.setTimeZone(tz);
			mDF_DateTimeHHMM.setTimeZone(tz);
			mDF_SimpleDateTime.setTimeZone(tz);
			mDF_SimpleDate.setTimeZone(tz);
			mDF_Date2.setTimeZone(tz);
			mDF_Date3.setTimeZone(tz);
		}
	}



	private static StringFormatter mInstance;
	private TimeZoneDateFormat mDF = null;
	private String mZoneTime = "";

	private StringFormatter(){
		mDF = new TimeZoneDateFormat();
	}

	/**
	 * 初始化时区
	 * @param zoneTime
     */
	public void initTime(int zoneTime){
		mDF.initTime(zoneTime);
	}

	/**
	 * 更新时区
	 * @param zoneTime
     */
	public void updateZone(int zoneTime){
//		mInstance = new StringFormatter();
		Logger.e("stringformatter updateZone = " + zoneTime);
		mDF.initTime(zoneTime);
	}

	public static StringFormatter instance(){
		if(null == mInstance){
			mInstance = new StringFormatter();
		}
		return mInstance;
	}

//	public static void reset(){
//		mInstance = null;
//	}

	/**
	 * 设置时区
	 * @return
     */
	public void setZoneTime(int zoneTime){
		String zone = "GMT";
		if(zoneTime >= 0){
			zone = zone + "+"+ zoneTime;
		}else{
			zone = zone + zoneTime;
		}
		mZoneTime = zone;
		Logger.e("stringformatter setZoneTime = " + mZoneTime);
	}

	/**
	 * 返回时区
	 * @return
     */
	public String getZoneTime(){
		Logger.e("stringformatter getZoneTime = " + mZoneTime);
		return mZoneTime;
	}


	/**
	 * 时间格式，HH:mm:ss
	 */
	public String toTime(Date date) {
		return mDF.mDF_Time.format(date);
	}

	/**
	 * 时间格式，HH:mm:ss
	 */
	public String secToTime(long seconds) {
		return toTime(new Date((long) seconds * 1000));
	}

	/**
	 * 时间格式，HH:mm:ss
	 */
	public String milToTime(long mils) {
		return toTime(new Date(mils));
	}

	/**
	 * 时间格式，HH:mm
	 */
	public String toTimeSimple(Date date) {
		return mDF.mDF_TimeSimple.format(date);
	}

	/**
	 * 时间格式，HH:mm
	 */
	public String secToTimeSimple(long seconds) {
		return toTimeSimple(new Date((long) seconds * 1000));
	}

	/**
	 * 时间格式，HH:mm
	 */
	public String milToTimeSimple(long mils) {
		return toTimeSimple(new Date(mils));
	}


	/**
	 * 时间格式，yyyy-MM-dd HH:mm:ss
	 */
	public String toDateTime(Date date) {
		return mDF.mDF_DateTime.format(date);
	}

	/**
	 * 时间格式，yyyy-MM-dd HH:mm
	 */
	public String toDateTimeHM(Date date) {
		return mDF.mDF_DateTimeHHMM.format(date);
	}

	/**
	 * 秒转时间，yyyy-MM-dd HH:mm:ss
	 */
	public String secToDateTime(long seconds) {
		return toDateTime(new Date((long) seconds * 1000));
	}

	/**
	 * 秒转时间，yyyy-MM-dd HH:mm
	 */
	public String secToDateTimeHM(long seconds) {
		return toDateTimeHM(new Date((long) seconds * 1000));
	}

	/**
	 * 秒转时间，yyyy-MM-dd HH:mm:ss
	 */
	public String milToDateTime(long mils) {
		return toDateTime(new Date(mils));
	}

	/**
	 * 时间格式，yyyy-MM-dd
	 */
	public String toDate(Date date) {
		return mDF.mDF_Date.format(date);
	}


	/**
	 * 秒转时间，yyyy-MM-dd
	 */
	public String secToDate(long seconds) {
		return toDate(new Date((long) seconds * 1000));
	}

	/**
	 * 秒转时间，yyyy-MM-dd
	 */
	public String milToDate(long mils) {
		return toDate(new Date(mils));
	}

	/**
	 * 时间格式，MM/dd HH:mm
	 */
	public String toSimpleDateTime(Date date) {
		return mDF.mDF_SimpleDateTime.format(date);
	}
	/**
	 * 时间格式，MM/dd HH:mm
	 */
	public String secSimpleDateTime(int seconds) {
		return toSimpleDateTime(new Date((long) seconds * 1000));
	}

	/**
	 * 时间格式，MM-dd HH:mm
	 */
	public String toSimpleDate(Date date) {
		return mDF.mDF_SimpleDate.format(date);
	}
	/**
	 * 时间格式，MM-dd HH:mm
	 */
	public String secSimpleDate(int seconds) {
		return toSimpleDate(new Date((long) seconds * 1000));
	}

	/**
	 * 秒转日期，yyyy/MM/dd
	 */
	public String secondToDate2(int seconds) {
		return mDF.mDF_Date2.format(new Date((long) seconds * 1000));
	}

	/**
	 * 秒转日期，yyyy/MM/dd HH:mm
	 */
	public String secondToDate3(int seconds) {
		return mDF.mDF_Date3.format(new Date((long) seconds * 1000));
	}

	/**
	 * 使用用户格式提取字符串日期
	 *
	 * @param strDate 日期字符串
	 * @param pattern 日期格式
	 * @return
	 */

	public static Date parse(String strDate, String pattern) {

		if (StringUtils.isEmpty(strDate)) {
			return null;
		}
		try {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			TimeZone timezone = TimeZone.getTimeZone(StringFormatter.instance().getZoneTime());
			df.setTimeZone(timezone);
			return df.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用用户格式格式化日期
	 *
	 * @param date    日期
	 * @param pattern 日期格式
	 * @return
	 */

	public static String format(Date date, String pattern) {
		String returnValue = "";
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			TimeZone timezone = TimeZone.getTimeZone(StringFormatter.instance().getZoneTime());
			df.setTimeZone(timezone);
			returnValue = df.format(date);
		}
		return (returnValue);
	}

	/**
	 * 使用用户格式格式化日期,GMT+0时区
	 *
	 * @param date    日期
	 * @param pattern 日期格式
	 * @return
	 */

	public static String formatTo0(Date date, String pattern) {
		String returnValue = "";
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			TimeZone timezone = TimeZone.getTimeZone("GMT+0");
			df.setTimeZone(timezone);
			returnValue = df.format(date);
		}
		return (returnValue);
	}

	/**
	 * 使用用户格式提取字符串日期
	 *
	 * @param strDate 日期字符串
	 * @param pattern 日期格式
	 * @return
	 */

	public static Date DateParse(String strDate, String pattern) {

		if (StringUtils.isEmpty(strDate)) {
			return null;
		}
		try {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 判断是否为今天(效率比较高)
	 *
	 * @param day 传入的 时间  "2017-05-31 10:10:30" "2017-05-31" 都可以
	 * @return true今天 false不是
	 * @throws ParseException
	 */
	public static boolean IsToday(String day) {

		if(StringUtils.isEmpty(day)){
			return false;
		}
		Calendar pre = Calendar.getInstance();
		Date predate = new Date(System.currentTimeMillis());
		TimeZone timezone = TimeZone.getTimeZone(StringFormatter.instance().getZoneTime());
		pre.setTimeZone(timezone);
		pre.setTime(predate);
		Calendar cal = Calendar.getInstance();
		Date date = DateParse(day,"yyyy-MM-dd");
		cal.setTime(date);
		if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
			int diffDay = cal.get(Calendar.DAY_OF_YEAR)
					- pre.get(Calendar.DAY_OF_YEAR);

			if (diffDay == 0) {
				return true;
			}
		}
		return false;
	}

}
